package com.mengwen.backstage.controller.admin;

import com.mengwen.backstage.pojo.*;
import com.mengwen.backstage.service.PcUserService;
import com.mengwen.backstage.service.RankService;
import com.mengwen.backstage.util.GetCharsUtil;
import com.mengwen.backstage.util.GetUserInfoUtil;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.keyinfo.KeyName;
import java.util.*;

/**
 * @author LiangYonghui
 * @date 2020/4/15 21:26
 * @description  后台管理员页面数据展示
 */
@Controller
@Slf4j
public class BackstageDataController {

    @Autowired
    RankService rankService;

    @Autowired
    PcUserService pcUserService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    GetUserInfoUtil getUserInfoUtil;

    @Autowired
    GetCharsUtil getCharsUtil;


    /**
     * type: 榜单类型（问卷数据0，(pc)萌问榜2，趣答榜1）
     * date: 日期类型（日1，周7，月30）
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/get_all_data/{type}/{date}")
    public BackstageDataDto adminGetAllData(@PathVariable("type") int type,@PathVariable("date") int date) throws Exception {
        // 判别date类型
        String keyName = null;

        RealTimeData realTimeData = null;

        log.info("date:" + date);
        log.info("type:" + type);

        if (Type.One == type) {
            if (date == DateType.DAY) {
                keyName = RankKeyName.ANSWER_DAY_KEY;
            } else if (date == DateType.MONTH) {
                keyName = RankKeyName.ANSWER_MONTH_KEY;
            } else {
                keyName = RankKeyName.ANSWER_WEEK_KEY;
            }
        } else if (Type.Two == type) {
            if (date == DateType.DAY) {
                keyName = RankKeyName.QUESTION_DAY_KEY;
            } else if (date == DateType.MONTH) {
                keyName = RankKeyName.QUESTION_MONTH_KEY;
            } else {
                keyName = RankKeyName.QUESTION_WEEK_KEY;
            }
        } else {
            // 查询平台实时问卷，以及实时在线人数，以及总问卷
            realTimeData = new RealTimeData(1124L, 1125L, 1126L);
        }

        List<Chart> charts = getCharsUtil.getCharts(keyName);

        BackstageDataDto backstageDataDto = new BackstageDataDto();

        return new BackstageDataDto(charts, realTimeData);

    }











    // ============================页面跳转================================
    @RequestMapping("/to_check_question")
    public String toCheckQestion(HttpServletRequest request, Model model){
        String admin_token = request.getParameter("admin_token");
        Claims claims = JwtUtil.checkJWT(admin_token);
        String role = (String) claims.get("role");

        if ("admin".equals(role)){
            model.addAttribute("adminToken",admin_token);
            log.info("to_check_question");
            return "check_question";
        }else {
            return "login_door";
        }


    }
    @RequestMapping("/to_now_data")
    public String toNowData(HttpServletRequest request,Model model){
        String admin_token = request.getParameter("admin_token");
        Claims claims = JwtUtil.checkJWT(admin_token);
        String role = (String) claims.get("role");

        if ("admin".equals(role)){
            model.addAttribute("adminToken",admin_token);
            log.info("进来to_now_data");
            return "data";
        }else {
            return "login_door";
        }


    }
    @RequestMapping("/to_help_publish")
    public String to_help_publish(HttpServletRequest request,Model model){
        String admin_token = request.getParameter("admin_token");
        Claims claims = JwtUtil.checkJWT(admin_token);
        String role = (String) claims.get("role");
        if ("admin".equals(role)){
            model.addAttribute("adminToken",admin_token);
            log.info("进来to_help_publish");
            return "create_question";
        }else {
            return "login_door";
        }



    }

    @RequestMapping("/to_anno")
    public String toAnno(HttpServletRequest request,Model model) {
        String admin_token = request.getParameter("admin_token");
        Claims claims = JwtUtil.checkJWT(admin_token);
        String role = (String) claims.get("role");
        if ("admin".equals(role)){
            model.addAttribute("adminToken",admin_token);
            log.info("to_anno");
            return "admin_anno";
        }else {
            return "login_door";
        }
    }

    @RequestMapping("/to_check_result")
    public String toCheckResult(){
        return "check_result";
    }
    @RequestMapping("/admin/return_management_page")
    public String return_management_page(HttpServletRequest request,Model model){
        String admin_token = request.getParameter("admin_token");
        System.out.println(admin_token);
        Claims claims = JwtUtil.checkJWT(admin_token);
        String role = (String) claims.get("role");

        if ("admin".equals(role)) {
            model.addAttribute("adminToken", admin_token);
            return "back_management_page";
        }else {
            return "login_door";
        }
    }








}
